{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-03-07T03:16:16.816296800Z",
     "start_time": "2024-03-07T03:16:16.664647900Z"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\""
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-07T03:16:16.882268700Z",
     "start_time": "2024-03-07T03:16:16.831335500Z"
    }
   },
   "id": "5a1b1b5515aadea9",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\""
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-07T03:16:16.885287100Z",
     "start_time": "2024-03-07T03:16:16.862617900Z"
    }
   },
   "id": "50a43563333d63c6",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Num GPUs Available:  0\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "print(\"Num GPUs Available: \", len(tf.config.experimental.list_physical_devices('GPU')))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-07T03:49:18.273970400Z",
     "start_time": "2024-03-07T03:49:10.958701100Z"
    }
   },
   "id": "8af13cbdc5251b47",
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "[]"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tensorflow.python.platform import build_info as tf_build_info\n",
    "\n",
    "# print(\"cuDNN Version:\", tf_build_info.cuda_version_number)\n",
    "tf.test.is_gpu_available()\n",
    "tf.config.experimental.list_physical_devices('GPU')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-07T04:02:40.867469Z",
     "start_time": "2024-03-07T04:02:40.826244200Z"
    }
   },
   "id": "712a3fb28c725a87",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Num GPUs Available:  0\n",
      "TensorFlow version:  2.10.0\n"
     ]
    }
   ],
   "source": [
    "\n",
    "print(\"Num GPUs Available: \", len(tf.config.experimental.list_physical_devices('GPU')))\n",
    "\n",
    "# 获取 TensorFlow 版本\n",
    "print(\"TensorFlow version: \", tf.__version__)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-07T05:37:32.223009Z",
     "start_time": "2024-03-07T05:37:32.206316300Z"
    }
   },
   "id": "a78ddef1e8aeb81a",
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "\n",
    "print(\"CUDA version: \", tf.config.experimental.version())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-03-07T03:16:19.647124400Z"
    }
   },
   "id": "7b71ef0734958871",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GPU Device Name:  \n"
     ]
    }
   ],
   "source": [
    "\n",
    "gpu_device = tf.test.gpu_device_name()\n",
    "print(\"GPU Device Name: \", gpu_device)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-07T05:37:22.802436600Z",
     "start_time": "2024-03-07T05:37:22.739514Z"
    }
   },
   "id": "7c1b86f1867b6841",
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "gpus = tf.config.experimental.list_physical_devices(device_type='GPU')\n",
    "cpus = tf.config.experimental.list_physical_devices(device_type='CPU')\n",
    "print(gpus, cpus)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-07T03:16:19.680809300Z",
     "start_time": "2024-03-07T03:16:19.654460200Z"
    }
   },
   "id": "46bbfed63c32742f",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "tf.config.list_physical_devices('GPU')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-03-07T03:16:19.658458300Z"
    }
   },
   "id": "a9f00bd9586db104",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "print(tf.test.is_built_with_cuda())\n",
    "print(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-03-07T03:16:19.662750600Z"
    }
   },
   "id": "662543e16fbd0ee7",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "physical_devices = tf.config.list_physical_devices('GPU')\n",
    "if len(physical_devices) == 0:\n",
    "    print(\"No GPU devices found.\")\n",
    "else:\n",
    "    # 尝试手动指定使用第一个 GPU 设备\n",
    "    tf.config.experimental.set_visible_devices(physical_devices[0], 'GPU')\n",
    "    print(\"Num GPUs Available: \", len(physical_devices))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-03-07T03:16:19.665750400Z"
    }
   },
   "id": "9591342da21bb896",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CUDA is available.\n",
      "Num GPUs Available:  1\n",
      "8700\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "# Check if CUDA (GPU support) is available\n",
    "if torch.cuda.is_available():\n",
    "    # Get the number of available GPUs\n",
    "    num_gpus = torch.cuda.device_count()\n",
    "    \n",
    "    print(\"CUDA is available.\")\n",
    "    print(\"Num GPUs Available: \", num_gpus)\n",
    "else:\n",
    "    print(\"CUDA is not available.\")\n",
    "print(torch.backends.cudnn.version())\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-07T04:04:27.713606Z",
     "start_time": "2024-03-07T04:04:27.670451300Z"
    }
   },
   "id": "707f412c23cb8290",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0'  # 设置 TensorFlow 日志级别\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-03-07T03:16:19.671267400Z"
    }
   },
   "id": "7615f19ab31940f5",
   "execution_count": null
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Num GPUs Available:  0\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "# TensorFlow 2.x 版本\n",
    "print(\"Num GPUs Available: \", len(tf.config.experimental.list_physical_devices('GPU')))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-07T04:42:04.493620500Z",
     "start_time": "2024-03-07T04:42:04.462454Z"
    }
   },
   "id": "4f9216730c1439d",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "d4e152461389fe5f"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "ecd5b4568f14b3ba"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "c6af5b08dc48ba41"
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "torch2",
   "language": "python",
   "display_name": "pytorch2.0-gpu"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
